package com.boarsoft.boar.soagov.com.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.entity.ComInfo;
import com.boarsoft.boar.entity.ObjFaces;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("comBiz")
public class ComBizImpl extends SimpleBizImpl implements ComBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<ComInfo> list(String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		// sb.append("and type!='").append(ComInfo.TYPE_SUBFLOW).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (code like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(ComInfo.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by name");
		}
		List<ComInfo> lt = dao.list(ComInfo.class, sb.toString(), pageNo, pageSize);
		for (ComInfo o : lt) {
			dao.evict(o);
		}
		return new PagedResult<ComInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<ComInfo> listByNexus(String by, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder().append(" from ").append(ComInfo.class.getName())
				.append(" c where exists (select n.objId1 from ").append(SoaNexus.class.getName())
				.append(" n where n.objId1=c.id and n.objId0='").append(by).append("')");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (c.code like '%").append(key);
			sb.append("%' or c.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(c)".concat(sb.toString()));
		//
		if (Util.strIsNotEmpty(orderBy)) {
			// String ob = Util.array2str(("e." + orderBy).replaceAll(",",
			// ",").split(","), ",");
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by c.code");
		}
		List<ComInfo> lt = dao.list("select c".concat(sb.toString()), pageNo, pageSize);
		// List<ComInfo> nlt = new ArrayList<ComInfo>();
		for (ComInfo o : lt) {
			// ComInfo n = (ComInfo) this.transfer(o, ComInfo.class);
			// nlt.add(n);
			dao.evict(o);
		}
		return new PagedResult<ComInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<ComInfo> listByFaceId(String faceId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(ComInfo.class.getName());
		sb.append(" c, ").append(ObjFaces.class.getName());
		sb.append(" o where c.id = o.objId and o.faceId='");
		sb.append(faceId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append("and (c.code like '%").append(key);
			sb.append("%' or c.name like '%").append(key);
			sb.append("%')");
		}
		String hql = "select count(c.id)".concat(sb.toString());
		Long total = (Long) dao.getValue(hql);
		if (Util.strIsNotEmpty(orderBy)) {
			String ob = Util.array2str(("c." + orderBy).replaceAll(",", ",").split(","), ",");
			sb.append(" order by ").append(ob);
		} else {
			sb.append(" order by c.name");
		}
		hql = "select c".concat(sb.toString());
		List<ComInfo> lt = dao.list(hql, pageNo, pageSize);
		return new PagedResult<ComInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void save(ComInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}

	@Override
	@Transactional
	public void update(ComInfo o) {
		dao.update(o);
	}

	@Override
	@Transactional
	public ReplyInfo<Object> delete(String id) {
		StringBuilder sb = new StringBuilder();
		// 检查是否有组件依赖这个组件
		sb.setLength(0);
		sb.append("and objId1='").append(id).append("'");
		int r = dao.getTotal(SoaNexus.class, sb.toString());
		if (r > 0) {
			return new ReplyInfo<Object>(Constants.INFO_INUSE);
		}
		// 删除组件间的依赖关系
		sb.setLength(0);
		sb.append("delete from ").append(SoaNexus.class.getName());
		sb.append(" where objId0='").append(id).append("'");
		dao.executeUpdate(sb.toString());
		// 删除和与接口的关系
		sb.setLength(0);
		sb.append("delete from ").append(ObjFaces.class.getName());
		sb.append(" where objId='").append(id).append("'");
		dao.executeUpdate(sb.toString());
		// 删除组件
		dao.delete(ComInfo.class, id);
		return ReplyInfo.SUCCESS;
	}

	@Override
	@Transactional(readOnly = true)
	public ComInfo get(String id) {
		return (ComInfo) dao.get(ComInfo.class, id);
	}

	@Override
	@Transactional
	public String copy(String sourceId, String parentId, String newId) {
		ComInfo o = this.get(sourceId);
		ComInfo n = (ComInfo) this.transfer(o, ComInfo.class);
		n.setId(newId);
		dao.save(n);

		// TODO copy ObjFaces

		// TODO copy SoaNexus

		return n.getId();
	}

	@Override
	@Transactional
	public boolean move(String id, String to) {
		// TODO 查找目标节点所属的库包，修改ComInfo的libId?
		return true;
	}
}